package net.mymilkedeek.ludum.tinythumb.model;

import org.newdawn.slick.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO: Write Documentation
 *
 * @author Michael Demey
 */
public class Battaljar {
    public static Party party1;
    public static Party party2;
    public static boolean victory = false;
    private static PartyFactory partyFactory;
    public static int turn = 0;
    public boolean boss = false;

    public void boss() {
        victory = false;
    }

    public Battaljar() {
        partyFactory = new PartyFactory();
        party1 = new Party();
        Member member = new Member();
        member.name = "Thumb";
        member.hp = 220;
        member.atk = 10;
        member.def = 20;
        member.lvl = 5;
        member.xptoNxt = 10;
        party1.setMember1(member);
        Member member2 = new Member();
        member2.name = "Chic";
        member2.hp = 180;
        member2.atk = 20;
        member2.def = 18;
        member2.lvl = 5;
        member2.xptoNxt = 10;
        party1.setMember2(member2);
    }

    public static List<String> attack(int choice, int choice2) {
        Log.debug( " " + choice + " and " + choice2);
        
        List<String> output = new ArrayList<String>();

        Member member = party2.getMember(choice);
        Log.debug(" pre-attack " + member.name);
        Log.debug(" pre-attack hp" + member.hp);
        Log.debug(" pre-attack def" + member.def);
        Log.debug(" pre-attack attack" + party1.getMember1().atk);
        if ( party1.getMember1().atk > member.def ) {
            member.hp += member.def - party1.getMember1().atk;
            output.add(member.name + " lost " + (member.def - party1.getMember1().atk) + " hp" );
        }
        Log.debug(" post-attack " + member.hp);

        Member member2 = party2.getMember(choice2);
        Log.debug(" pre-attack " + member2.name);
        Log.debug(" pre-attack hp" + member2.hp);
        Log.debug(" pre-attack def" + member2.def);
        Log.debug(" pre-attack attacker" + party1.getMember2().atk);
        if ( party1.getMember2().atk > member2.def ) {
            member2.hp += member2.def - party1.getMember2().atk;
            output.add(member2.name + " lost " + (member2.def - party1.getMember2().atk) + " hp" );
        }
        Log.debug(" post-attack " + member2.hp);
        
        double random = Math.random();
        double random2 = Math.random();
        
        if ( random * 1000 < 500) {
            if (party1.getMember1().def < party2.getMember1().atk ) {
                party1.getMember1().hp -= party1.getMember1().def - party2.getMember1().atk;
                output.add(party1.getMember1().name + " lost " + (party1.getMember1().def - party2.getMember1().atk) + " hp" );
            }
        } else {
            party1.getMember2().hp -= party1.getMember2().def - party2.getMember1().atk;
            output.add(party1.getMember2().name + " lost " + (party1.getMember2().def - party2.getMember1().atk) + " hp" );
        }

        if ( random2 * 1000 > 500 && party2.getMember2() != null) {
            party1.getMember1().hp -= party1.getMember1().def - party2.getMember2().atk;
            output.add(party1.getMember1().name + " lost " + (party1.getMember1().def - party2.getMember2().atk) + " hp" );
        } else if ( random2 * 1000 < 500 && party2.getMember2() != null) {
            party1.getMember2().hp -= party1.getMember2().def - party2.getMember2().atk;
            output.add(party1.getMember2().name + " lost " + (party1.getMember2().def - party2.getMember2().atk) + " hp" );
        }
        
        Log.debug(" thumb " + party1.getMember1().hp);
        Log.debug(" chicken " + party1.getMember2().hp);

        turn++;

        return output;
    }

    public static void initParty2() {
        if ( partyFactory == null ) {
            partyFactory = new PartyFactory();
        }
        party2 = partyFactory.letsGetThisPartyStarted();
    }

    public static void end() {
        Log.debug("end battle");
        turn = 0;
        party2 = null;
        victory = true;
    }
}